<?php
/**
 * Created by PhpStorm.
 * Author: huangwy
 * Date: 2018/3/15 16:16
 * Version: 1.0
 */
namespace app\admin\controller;

use think\Db;
use think\facade\Request;
use think\File;

class Upload extends Base
{
    public function index($type = null)
    {

    }

    public function uploadpic()
    {
        $Img = input('Img');
//        dump($_FILES);die;
        $Path = null;
        if ($_FILES['img']) {
            $Img = $this->saveimg($_FILES);

//            dump($Img);die;
        }
        $BackCall = input('BackCall');
        $Width = input('Width');
        $Height = input('Height');
        if (!$BackCall) {
            $Width = $_POST['BackCall'];
        }
        if (!$Width) {
            $Width = $_POST['Width'];
        }
        if (!$Height) {
            $Width = $_POST['Height'];
        }
        $this->assign('Width', $Width);
        $this->assign('BackCall', $BackCall);
        $this->assign('Img', $Img);
        $this->assign('Height', $Height);
        return view('Uploadpic');
    }

    private function saveimg($files)
    {
        $mimes = array(
            'image/jpeg',
            'image/jpg',
            'image/jpeg',
            'image/png',
            'image/pjpeg',
            'image/gif',
            'image/bmp',
            'image/x-png'
        );
        $exts = array(
            'jpeg',
            'jpg',
            'jpeg',
            'png',
            'pjpeg',
            'gif',
            'bmp',
            'x-png'
        );

        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('img');

        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
//            $info = $file->move('attached');
            $info = $file->rule('date')->validate(['size'=>1048576,'ext'=>$exts])->move( 'attached');
            if($info){
                $filePath = '/attached/'.$info->getSaveName();//输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                //$filePath = '/attached/'.$info->getExtension();// 输出 jpg
                //$filePath = '/attached/'.$info->getFilename();// 输出 42a79759f284b767dfcb2a0197904287.jpg
            }else{
                // 上传失败获取错误信息
                $error = $file->getError();
                echo "<script>alert('{$error}')</script>";
            }
        }
        $ImgStr = $filePath;
        return $ImgStr;

    }

    public function batchpic()
    {
        $ImgStr = input('Img');
        $ImgStr = trim($ImgStr, '|');
        $Img = array();
        if (strlen($ImgStr) > 1) {
            $Img = explode('|', $ImgStr);
        }
        $Path = null;
        $newImg = array();
        $newImgStr = null;
        if ($_FILES) {
            $newImgStr = $this->saveimg($_FILES);
            if ($newImgStr) {
                $newImg = explode('|', $newImgStr);
            }

        }
        $Img = array_merge($Img,$newImg);
        $ImgStr = implode("|", $Img);
        $BackCall = input('BackCall');
        $Width = input('u');
        $Height = input('Height');
        if (!$BackCall) {
            $Width = $_POST['BackCall'];
        }
        if (!$Width) {
            $Width = $_POST['Width'];
        }
        if (!$Height) {
            $Width = $_POST['Height'];
        }
        $this->assign('Width', $Width);
        $this->assign('BackCall', $BackCall);
        $this->assign('ImgStr', $ImgStr);
        $this->assign('Img', $Img);
        $this->assign('Height', $Height);
        return view('Batchpic');
    }
}
